package edu.uoc.tdp.pac4.client.logandreg;

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.rmi.RemoteException;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JTextField;

import edu.uoc.tdp.pac4.beans.Usuari;
import edu.uoc.tdp.pac4.client.Client;
import edu.uoc.tdp.pac4.client.ClientConnectionHelper;
import edu.uoc.tdp.pac4.common.util.LanguageUtils;


/**
 * Classe per a fer el login
 * @author oscarmateu
 *
 */
public class LoginWindow extends JFrame {

	private static final long serialVersionUID = 1L;
	private JPasswordField passwordField;
	private JTextField textField;
	
	ClientConnectionHelper connectionHelper = null;
	
	public LoginWindow() {
		
		super(LanguageUtils.getMessage("login.title"));
		getContentPane().setLayout(null);
		setBounds(100, 100, 400, 300);
		
		connectionHelper = new ClientConnectionHelper();
		//Demo:
		//connectionHelper.getStatsManager().dummyMethod("param");
		
		passwordField = new JPasswordField();
		passwordField.setBounds(154, 114, 115, 20);
		getContentPane().add(passwordField);
		
		JLabel lblUser = new JLabel("User");
		lblUser.setBounds(55, 86, 80, 14);
		getContentPane().add(lblUser);
		
		JLabel lblPassword = new JLabel("Password");
		lblPassword.setBounds(55, 117, 80, 14);
		getContentPane().add(lblPassword);
		
		textField = new JTextField();
		textField.setBounds(154, 83, 115, 20);
		getContentPane().add(textField);
		textField.setColumns(10);
		
		JButton btnAccept = new JButton("Accept");
		btnAccept.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				EventQueue.invokeLater(new Runnable() {
					public void run() {
						try {
							
							@SuppressWarnings("deprecation")
							Usuari usr = connectionHelper.getLogAndRegManager().login(textField.getText(), passwordField.getText());					
							if(usr != null){
								ClientConnectionHelper.setLoggedUser(usr);
								Client frame = new Client(connectionHelper);
								frame.setVisible(true);
							}else{
								JOptionPane.showMessageDialog(null, 
										LanguageUtils.getMessage("client.message.error.login"),
										LanguageUtils.getMessage("client.message.error.title"),
										JOptionPane.ERROR_MESSAGE);
							}
							
						} catch (RemoteException e) {
							JOptionPane.showMessageDialog(null, 
									LanguageUtils.getMessage("client.error.message.remote"),
									LanguageUtils.getMessage("client.error.title"),
									JOptionPane.ERROR_MESSAGE);
						}catch (NullPointerException n){
							JOptionPane.showMessageDialog(null, 
									LanguageUtils.getMessage("client.message.error.login"),
									LanguageUtils.getMessage("client.message.error.title"),
									JOptionPane.ERROR_MESSAGE);
						}
					}
				});
			}
		});
		btnAccept.setBounds(82, 160, 89, 23);
		getContentPane().add(btnAccept);
		
		JButton btnCancel = new JButton("Cancel");
		btnCancel.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				 System.exit(0);  
			}
		});
		btnCancel.setBounds(215, 160, 89, 23);
		getContentPane().add(btnCancel);
		
		JLabel lblNewLabel = new JLabel("Please insert your user and password");
		lblNewLabel.setFont(new Font("Arial", Font.PLAIN, 15));
		lblNewLabel.setForeground(Color.BLUE);
		lblNewLabel.setBounds(55, 47, 310, 14);
		getContentPane().add(lblNewLabel);
	}
	
	public static void main(String[] args) {
		
		LanguageUtils.setDefaultLanguage("i18n/messages");
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					LoginWindow frame = new LoginWindow();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
	
	
}
